﻿using AutoMapper;
using MediatR;
using Provenance.Domain.Breed;
using Provenance.ErrorCount;
using Provenance.Infra;
using Provenance.Infra.Interfaces.Breed;
using Provenance.Read.Api.Application.Command.Breed;

namespace Provenance.Read.Api.Application.CommandHandler.Breed
{
    public class BackBreedEnterpriseCommandHandler : IRequestHandler<BackBreedEnterpriseCommand, ApiResult<BreedEnterprise>>
    {
        private readonly IBreedEnterpriseRepository _breeEnterpriseRepository;
        private readonly ILogger<BackBreedEnterpriseCommandHandler> _logger;
        private readonly IMapper _mapper;
        public BackBreedEnterpriseCommandHandler(IBreedEnterpriseRepository breedEnterpriseRepository, ILogger<BackBreedEnterpriseCommandHandler> logger, IMapper mapper)
        {
            _breeEnterpriseRepository = breedEnterpriseRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult<BreedEnterprise>> Handle(BackBreedEnterpriseCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var obj = await _breeEnterpriseRepository.GetAsync(x => x.ProductInfoId == request.ProductInfoId);
                if(obj == null)
                {
                    return ApiResult<BreedEnterprise>.Failure("Breed not found");
                }
                return ApiResult<BreedEnterprise>.Success(obj);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error getting breed enterprise"+ex.Message);
                return ApiResult<BreedEnterprise>.Failure("Error getting breed enterprise"+ex.Message);
            }
        }
    }
}
